package net.kunyv.DAL;

import net.kunyv.entity.DaoEntity;
import net.kunyv.entity.DbConfig;
import net.kunyv.entity.EntityColumn;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class OracleInitBeanImpl implements InitBean
{
    private DbConfig dbconfig;

    public List<EntityColumn> init(DbConfig dbconfig, DaoEntity de) throws Exception {
        this.dbconfig = dbconfig;
        Connection conn = null;
        List<EntityColumn> list = new ArrayList<>();
        String strsql = null;

        if (de.isCustomSql()) {
            strsql = de.getSql();
        } else {
            strsql = "select * from " + de.getTableName() + " where rownum<1";
        }

        String tablecomment = getTableComment(de);
        if (tablecomment != null) {
            de.setTableComment(tablecomment);
        } else {
            de.setTableComment("");
        }
        if (de.getEntityName() != null);


        HashMap<String, String> commentmap = getComments(de); try {
            String keyColumn;
            conn = DbSession.getConnection(dbconfig);
            PreparedStatement pstmt = conn.prepareStatement(strsql);
            pstmt.execute();
            ResultSetMetaData rsmd = pstmt.getMetaData();
            int size = rsmd.getColumnCount();


            if (de.isCustomSql()) {
                keyColumn = "";
            } else {
                keyColumn = getKeyColumns(de);
            }


            for (int i = 0; i < size; i++) {
                EntityColumn ec = new EntityColumn();
                String columnName = rsmd.getColumnName(i + 1);
                String columnTypName = rsmd.getColumnTypeName(i + 1);
                System.out.print("column:" + columnName + "    ");
                System.out.print("columnclass:" + rsmd.getColumnClassName(i + 1) + "    ");
                System.out.print("ColumnTyp:" + columnTypName + "    ");
                System.out.println("ColumnSize:" + rsmd.getColumnDisplaySize(i + 1) + "    ");
                if (rsmd.isNullable(i + 1) == 0) {
                    ec.setRequired(true);
                }
                if (columnName.equalsIgnoreCase(keyColumn)) {
                    ec.setKey(true);
                }
                ec.setMybatis(de.isMybatis());
                ec.setDatabaseName(de.getDatabaseName());

                ec.setColumnNam(columnName);

                if (rsmd.getPrecision(i + 1) == 0) {
                    if (!columnTypName.equalsIgnoreCase("DATE") && !columnTypName.equalsIgnoreCase("TIMESTAMP")) {
                        ec.setColumnTyp("DECIMAL");
                    } else {
                        ec.setColumnTyp(columnTypName);
                    }
                } else {
                    ec.setColumnTyp(columnTypName);
                }


                if (commentmap == null) {
                    ec.setEntityColumnComment(getColComment(ec.getColumnNam(), de));
                } else {
                    ec.setEntityColumnComment(commentmap.get(ec.getColumnNam().toUpperCase()));
                }

                ec.setPrecision(rsmd.getPrecision(i + 1));
                ec.setScale(rsmd.getScale(i + 1));
                list.add(ec);
            }
        } catch (SQLException e) {
            throw new Exception(e);
        } finally {
            DbSession.closeConnection(conn);
        }
        return list;
    }







    private HashMap<String, String> getComments(DaoEntity de) {
        HashMap<String, String> map = new HashMap<>();

        if (de.isCustomSql()) {
            return null;
        }
        map = getColComments(de);

        return map;
    }

    private String getTableComment(DaoEntity de) {
        String tablecomment = "";
        Connection conn = DbSession.getConnection(this.dbconfig);

        String sql = "SELECT * FROM USER_TAB_COMMENTS where TABLE_NAME='" + de.getTableName().toUpperCase() + "'";
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            if (rs.isAfterLast() == rs.isBeforeFirst()) {
                return null;
            }
            while (rs.next()) {
                tablecomment = rs.getString("COMMENTS");
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
            DbSession.closeConnection(conn);
        }

        return tablecomment;
    }




    private HashMap<String, String> getColComments(DaoEntity de) {
        Connection conn = DbSession.getConnection(this.dbconfig);

        String sql = "SELECT * FROM USER_COL_COMMENTS where TABLE_NAME='" + de.getTableName().toUpperCase() + "'";
        HashMap<String, String> map = new HashMap<>();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            if (rs.isAfterLast() == rs.isBeforeFirst()) {
                return null;
            }
            while (rs.next())
            {
                if (null == rs.getString(3) || "".equals(rs.getString(3))) {
                    map.put(rs.getString(2), getColComment(rs
                            .getString(2), de)); continue;
                }
                map.put(rs.getString(2), rs.getString(3));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
            DbSession.closeConnection(conn);
        }

        return map;
    }







    private String getColComment(String column, DaoEntity de) {
        Connection conn = DbSession.getConnection(this.dbconfig);
        String sql = "SELECT distinct COLUMN_NAME,COMMENTS FROM USER_COL_COMMENTS where COLUMN_NAME='" + column + "' AND ROWNUM<2 AND COMMENTS IS NOT NULL ORDER BY COMMENTS";


        String s = "";
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                s = rs.getString(2);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
            DbSession.closeConnection(conn);
        }

        return s;
    }









    private String getKeyColumns(DaoEntity de) {
        Connection conn = DbSession.getConnection(this.dbconfig);

        String sql = "select b.column_name from user_constraints a,user_cons_columns b where a.constraint_name=b.constraint_name and a.table_name='" + de.getTableName() + "' and a.constraint_type='P'";

        String key = "";
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                key = rs.getString(1);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
            DbSession.closeConnection(conn);
        }

        return key;
    }


    public static void main(String[] args) {
        int i = 1000;
        double result = 1000.0D;
        double mi = 0.0D;

        while (i != 1) {


            i--;
            result *= Double.parseDouble(String.valueOf(i));
            if (result / 1.0E10D > 1.0D) {
                mi += 10.0D;
                result /= 1.0E10D;
            }
        }
        System.out.println(result);
        System.out.println(mi);
        System.out.println("departure".toUpperCase());
    }
}
